<template>
  <div>
    <el-container class="container">
      <el-header>
        <span class="title">校园二手交易市场</span>
      </el-header>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="verify">
            <el-input v-model="ruleForm.verify" class="verifyInput"></el-input>
            <img :src="url" alt="验证码" class="verifyImg" @click="getVerify">
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button @click="registerRouter">注册</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    var checkVerify = (rule, value, callback) => {
      if(value === ''){
        callback(new Error('请输入验证码'))
      } else if(value !== this.verifyCode) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username:'',
        password:'',
        verify:''
      },
      verifyCode: '',
      url: '',
      randomNum: '',
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        verify: [
          { validator: checkVerify, trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/login', {
            username: this.ruleForm.username,
            password: this.ruleForm.password
          }).then(res => {
            const jwt = res.headers['authorization']
            const userInfo = res.data.data
            this.$store.commit('setToken', jwt)
            this.$store.commit('setUserInfo', userInfo)
            if(userInfo.level === 1){
              this.$router.push('/manager')
            } else {
              this.$router.push('/home')
            }
          })
        } else {
          console.log('error submit!!');
          this.getVerify()
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getVerify(){
      this.randomNum = parseInt(Math.random()*50)
      const url = `http://localhost:8081/verify?num=${this.randomNum}`
      this.url = this.timeStamp(url)
      this.$axios.get(`/code?num=${this.randomNum}`)
          .then(res => {
            this.verifyCode = res.data.data
          })
    },
    timeStamp(url){
      var getTimeStamp = new Date().getTime()
      return url + '&timestamp' + getTimeStamp
    },
    registerRouter(){
      this.$router.push('/register')
    }
  },
  created() {
    this.getVerify()
  }
}
</script>

<style scoped>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.container{
  max-width: 960px;
  margin: 0 auto;
}
.title{
  font-size: 50px;
}
.demo-ruleForm{
  max-width: 400px;
  margin: 0 auto;
}
.verifyInput{
  position: absolute;
  left: 0;
  width: 150px;
}
.verifyImg{
  position: absolute;
  margin-left: 30px;
  width: 90px;
}
</style>